package me.chnxan.biz;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public abstract class BussinessController {
	
	public abstract Map<String, Object> getModels(HttpServletRequest request) throws Exception;
	public abstract void execute(HttpServletRequest request) throws Exception;
	
	@RequestMapping(params = "m=submit")
	protected void submit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setCharacterEncoding("UTF-8");
		try {
			if(request.getSession().getAttribute("currentUser") == null) {
				//没有登陆;
				return;
			}
			execute(request);
			response.sendRedirect(request.getParameter("vid") + ".do?m=show");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "m=show")
	protected ModelAndView show(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setCharacterEncoding("UTF-8");
		Map<String, Object> models = null;
		try {
			if(request.getSession().getAttribute("currentUser") == null) {
				//没有登陆;
				return null;
			}
			models = getModels(request);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(models.get("viewName").toString(), models);
	}
	
}
